package com.example.timetable;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.example.timetable.databinding.ActivityMainBinding;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import site.gemus.openingstartanimation.LineDrawStrategy;
import site.gemus.openingstartanimation.OpeningStartAnimation;


/**
 * 主页面和欢迎界面的活动
 */
public class MainActivity extends AppCompatActivity {


    /**
     * 这个是viewBinding可以直接通过id找到对应的控件进行操作省去findViewById操作
     */
    private ActivityMainBinding binding;
    private static List<String> sPermissions = new ArrayList<String>(){};
    private static int MAX_NUMBER_REQUEST_PERMISSIONS = 2;
    private static int REQUEST_CODE_PERMISSIONS = 101;
    private int permissionRequestCount = 0;
    private boolean hasPermissions = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        LitePal.getDatabase();
        splash();
        //绑定BottomNavigationView（底部导航控件）和fragment(碎片控件)
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupWithNavController(binding.navView, navController);
        sPermissions.add(Manifest.permission.INTERNET);
        sPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        sPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
//        requestPermissionsIfNecessary();
    }

//    /**
//     * 权限申请
//     */
//    private void requestPermissionsIfNecessary() {
//        hasPermissions = checkAllPermissions();
//        if (!hasPermissions) {
//            if (permissionRequestCount < MAX_NUMBER_REQUEST_PERMISSIONS) {
//                permissionRequestCount += 1;
//                ActivityCompat.requestPermissions(this, sPermissions.toArray(new String[0]), REQUEST_CODE_PERMISSIONS);
//            } else {
//
//            }
//        }
//    }
//
//    /**
//     * 权限回调 onActivityResult已经废弃
//     */
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        if (requestCode == REQUEST_CODE_PERMISSIONS) {
//            requestPermissionsIfNecessary();
//        }
//    }
//
//    /**
//     *  权限检查
//     * @return 权限是否申请
//     */
//    private Boolean checkAllPermissions() {
//        boolean hasPermissions = true;
//        for (String permission : sPermissions) {
//            hasPermissions = hasPermissions & (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED);
//        }
//        return hasPermissions;
//    }


    /**
     * 欢迎界面配置请参考 https://github.com/wongzy/FancyView
     */
    private void splash(){
        //通过图片工厂(BitmapFactory)获取bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        //创建一直合适的空白，大小100*100的图片
        Bitmap newBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        //初始化画布
        Canvas canvas = new Canvas(newBitmap);
        //将图片缩小到合适位置
        canvas.drawBitmap(bitmap, null, new RectF(0, 0, 100, 100), null);
        //通过(BitmapDrawable)转换成BitmapDrawable因为Drawable是BitmapDrawable的父类所以直接转换为Drawable
        BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),newBitmap);
        //防止oom回收图片
        bitmap.recycle();
        OpeningStartAnimation openingStartAnimation = new OpeningStartAnimation.Builder(this)
                .setDrawStategy(new LineDrawStrategy())//设置界面样式
                .setAppIcon(bitmapDrawable) //设置图片
                .setColorOfAppIcon(getColor(R.color.purple_200)) //设置绘制图片线条的颜色
                .setAppName(getString(R.string.app_name)) //设置app名字
                .setColorOfAppName(getColor(R.color.purple_200)) //设置app名字颜色
                .setAppStatement("千里之行,积于跬步") //设置描述（不能太长）
                .setColorOfAppStatement(Color.BLACK) // 设置描述的颜色
                .setAnimationInterval(4000) // 设置动画的时间间隔
                .setAnimationFinishTime(500) // 设置动画的消失时间段
                .create();
        //启动欢迎界面
        openingStartAnimation.show(this);
    }
}